package com.clstu.socket;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPFileCopyServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("服务端在8888端口监听，等待连接......");
        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream("src\\mkbk01.jpg");//写入位置
        int realLen =0;
        byte[] buf = new byte[1024];
        while ((realLen = inputStream.read(buf))!=-1){
            fileOutputStream.write(buf,0,realLen);
        }
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello，我是服务端, 你发送的图片收到了".getBytes());//发送收到了你的图片
        socket.shutdownOutput();

        fileOutputStream.close();
        inputStream.close();
        outputStream.close();
        socket.close();
        serverSocket.close();
        System.out.println("server 端已结束~~~~~~~~~~");
    }
}
